using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace AuditionApp.Pages.QuestionBank
{
    public partial class ComprehensiveAnalysisListPage : Page
    {
        public class QuestionItem
        {
            public string Title { get; set; } = string.Empty;
            public List<string> Question { get; set; } = new List<string>();
            public string? Source { get; set; }
            public List<string>? Analysis { get; set; }
            public List<string>? Answer { get; set; }
            public List<MotiveTemplatePage.ChildQuestion>? Children { get; set; }
        }

        private ItemsControl questionsList;

        public ComprehensiveAnalysisListPage()
        {
            InitializeComponent();
            questionsList = (ItemsControl)FindName("QuestionsList");
            LoadQuestions();
        }

        private void LoadQuestions()
        {
            var questions = new List<QuestionItem>();
            var directory = Path.Combine(Directory.GetCurrentDirectory(), "Pages", "QuestionBank", "ComprehensiveAnalysis");
            
            if (Directory.Exists(directory))
            {
                var files = Directory.GetFiles(directory, "*.yaml").OrderBy(f => f);
                foreach (var file in files)
                {
                    try
                    {
                        var yaml = File.ReadAllText(file);
                        var deserializer = new DeserializerBuilder()
                            .WithNamingConvention(CamelCaseNamingConvention.Instance)
                            .IgnoreUnmatchedProperties()
                            .Build();
                        var question = deserializer.Deserialize<QuestionItem>(yaml);
                        if (question != null)
                        {
                            // 处理转义字符
                            question.Title = UnescapeString(question.Title);
                            if (question.Question != null)
                                question.Question = question.Question.Select(q => UnescapeString(q)).ToList();
                            if (question.Source != null)
                                question.Source = UnescapeString(question.Source);
                            if (question.Analysis != null)
                                question.Analysis = question.Analysis.Select(a => UnescapeString(a)).ToList();
                            if (question.Answer != null)
                                question.Answer = question.Answer.Select(a => UnescapeString(a)).ToList();
                            if (question.Children != null)
                            {
                                foreach (var child in question.Children)
                                {
                                    child.Title = UnescapeString(child.Title);
                                    if (child.Question != null)
                                        child.Question = child.Question.Select(q => UnescapeString(q)).ToList();
                                    if (child.Source != null)
                                        child.Source = UnescapeString(child.Source);
                                    if (child.Analysis != null)
                                        child.Analysis = child.Analysis.Select(a => UnescapeString(a)).ToList();
                                    if (child.Answer != null)
                                        child.Answer = child.Answer.Select(a => UnescapeString(a)).ToList();
                                }
                            }
                            questions.Add(question);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"加载文件 {file} 时出错: {ex.Message}");
                    }
                }
            }

            questionsList.ItemsSource = questions;
        }

        private string UnescapeString(string input)
        {
            if (string.IsNullOrEmpty(input)) return input;
            return input.Replace("\\\"", "\"").Replace("\\\\", "\\");
        }

        private void QuestionButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.DataContext is QuestionItem question)
            {
                try
                {
                    var questionPage = new MotiveTemplatePage();
                    var children = question.Children ?? new List<MotiveTemplatePage.ChildQuestion>();
                    
                    questionPage.LoadQuestion(
                        question.Title,
                        string.Join("\n", question.Question),
                        string.Join("\n", question.Analysis ?? new List<string>()),
                        children,
                        string.Join("\n", question.Answer ?? new List<string>()),
                        question.Source
                    );
                    NavigationService.Navigate(questionPage);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"加载问题时出错: {ex.Message}\n{ex.StackTrace}");
                }
            }
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
    }
} 
